﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Library.Controls
{
    /// <summary>
    /// Interaction logic for OptionsView.xaml
    /// </summary>
    public sealed partial class OptionsView
    {
        public OptionsView()
        {
            InitializeComponent();
        }

        #region IsOpen
        /// <summary>
        /// IsOpen Dependency Property
        /// </summary>
        private static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(OptionsView),
                new FrameworkPropertyMetadata(false,
                    new PropertyChangedCallback(OnIsOpenChanged)));

        /// <summary>
        /// Gets or sets the IsOpen property.  This dependency property 
        /// indicates ....
        /// </summary>
        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            private set { SetValue(IsOpenProperty, value); }
        }

        /// <summary>
        /// Handles changes to the IsOpen property.
        /// </summary>
        private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((OptionsView)d).OnIsOpenChanged(e);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the IsOpen property.
        /// </summary>
        private void OnIsOpenChanged(DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                Visibility = Visibility.Visible;
                var open = Resources["Open"] as Storyboard;
                if (open != null) open.Begin(this);
            }
            else
            {
                Visibility = Visibility.Visible;
                var close = Resources["Close"] as Storyboard;

                EventHandler handler = null;
                handler = (object s, EventArgs sbe) =>
                            {
                                if (close != null) close.Completed -= handler;
                                Visibility = Visibility.Collapsed;
                            };

                if (close != null)
                {
                    close.Completed += handler;
                    close.Begin(this);
                }
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IsOpen = false;
        }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsOpen = false;
        }

        public void SetParameters()
        {
            OptionsViewModel.Current.SetParameters();
        }
    }
}
